package system_code.CopyFile;

import java.io.*;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        //srcFile是复制的文件
        //destFile是被复制的目标文件
        System.out.println("请输入源文件:");
        Scanner scanner = new Scanner(System.in);
        File srcFile = new File(scanner.next());
        if(!srcFile.isFile()){
            System.out.println("输入源文件有误~");
            return;
        }
        System.out.println("请输入目标文件:");
        File destFile = new File(scanner.next());
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("目标文件输入错误~");
            return;
        }
        //打开源文件，将其内容读取出来放入目标文件中
        try(OutputStream outputStream = new FileOutputStream(destFile);
                InputStream inputStream = new FileInputStream(srcFile)){
            //字节流读取内容
            while(true){
                int ret = inputStream.read();
                if(ret == -1){
                    System.out.println("复制完成~");
                    return;
                }
                outputStream.write(ret);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    
}
